<?php
/**
 * CTuiModal class file.
 *
 * @author Sebastian Thierer <sebathi@gmail.com>
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

Yii::import('zii.widgets.tui.CTuiWidget');

/**
 * CTuiModal displays a dialog widget.
 *
 * CTuiModal encapsulates the {@link http://jqueryui.com/demos/dialog/ JUI Dialog}
 * plugin.
 *
 * To use this widget, you may insert the following code in a view:
 * <pre>
 * $this->beginWidget('zii.widgets.jui.CTuiModal', array(
 *     'id'=>'mydialog',
 *     // additional javascript options for the dialog plugin
 *     'options'=>array(
 *         'title'=>'Dialog box 1',
 *         'autoOpen'=>false,
 *     ),
 * ));
 *
 *     echo 'dialog content here';
 *
 * $this->endWidget('zii.widgets.jui.CTuiModal');
 *
 * // the link that may open the dialog
 * echo CHtml::link('open dialog', '#', array(
 *    'onclick'=>'$("#mydialog").dialog("open"); return false;',
 * ));
 * </pre>
 *
 * By configuring the {@link options} property, you may specify the options
 * that need to be passed to the JUI dialog plugin. Please refer to
 * the {@link http://jqueryui.com/demos/dialog/ JUI Dialog} documentation
 * for possible options (name-value pairs).
 *
 * @author Sebastian Thierer <sebathi@gmail.com>
 * @version $Id: CTuiModal.php 2805 2011-01-03 16:33:46Z qiang.xue $
 * @package zii.widgets.jui
 * @since 1.1
 */
class CTuiModal extends CTuiWidget
{
	/**
	 * @var string the name of the container element that contains all panels. Defaults to 'div'.
	 */
	public $tagName='div';

	/**
	 * Renders the open tag of the dialog.
	 * This method also registers the necessary javascript code.
	 */
	public function init()
	{
		parent::init();

		$id=$this->getId();
		if (isset($this->htmlOptions['id']))
			$id = $this->htmlOptions['id'];
		else
			$this->htmlOptions['id']=$id;

		if (isset($this->options['class']))
			$this->htmlOptions['class'] = $this->options['class'];
		else
			$this->htmlOptions['class']='modal fade hide';

		$this->htmlOptions['style'] = '';
		if (isset($this->options['width']))
			$this->htmlOptions['style'] .= 'width:'.$this->options['width'].'px;';
		if (isset($this->options['height']))
			$this->htmlOptions['style'] .= 'height:'.$this->options['height'].'px;';

		//$options=empty($this->options) ? '' : CJavaScript::encode($this->options);
		// Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').modal($options);");
		$header = '<div class="modal-header">';
		$header .= '<a class="close" data-dismiss="modal">×</a>';
		$header .= '<h3>'.$this->options["title"].'</h3>';
		$header .= '</div>';
		echo CHtml::openTag($this->tagName,$this->htmlOptions)."\n".$header;
	}

	/**
	 * Renders the close tag of the dialog.
	 */
	public function run()
	{
		echo CHtml::closeTag($this->tagName);
	}
}
